package com.example.onlinemusic.service;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.mapper.MusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoveMusicService {

    @Resource
    private LoveMusicMapper loveMusicMapper;
    @Autowired
    private MusicMapper musicMapper;

    public ResponseBodyMessage<Boolean> likeMusic(String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        System.out.println("musicId：" + musicId);
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登陆");
            return new ResponseBodyMessage<>(-1, "用户未登录", false);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId：" + userId);
        // 查询当前用户是否收藏过该音乐
        Music loveMusic = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId, musicId);
        if (loveMusic != null) {
            // 收藏过不能再收藏  TODO：[作业]加一个取消收藏的功能
            return new ResponseBodyMessage<>(-1, "该音乐已经被收藏过", false);
        }
        boolean effect = loveMusicMapper.insertLoveMusic(userId, musicId);
        if (effect) {
            return new ResponseBodyMessage<>(0, "收藏成功", true);
        }
        return new ResponseBodyMessage<>(-1, "收藏失败", false);
    }

    public ResponseBodyMessage<List<Music>> findLoveMusic(String musicName, HttpServletRequest request) {
        // 判断登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登陆");
            return new ResponseBodyMessage<>(-1, "用户未登录", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId：" + userId);
        List<Music> musicList = null;
        if (musicName == null) {
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        } else {
            musicList = loveMusicMapper.findLoveMusicByKeyAndUserId(musicName, userId);
        }
        return new ResponseBodyMessage<>(0, "查询到了所有的歌曲信息", musicList);
    }

    public ResponseBodyMessage<Boolean> deleteLoveMusic(String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登陆");
            return new ResponseBodyMessage<>(-1, "用户未登录", false);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        int ret = loveMusicMapper.deleteLoveMusic(userId, musicId);
        if (ret == 1) {
            return new ResponseBodyMessage<>(0, "取消收藏成功", true);
        } else {
            return new ResponseBodyMessage<>(-1, "取消收藏失败", false);
        }
    }
}
